package com.bjtime.designer.view

import android.content.Context
import android.support.v4.view.ViewPager
import android.util.AttributeSet
import android.view.MotionEvent
import android.icu.lang.UCharacter.GraphemeClusterBreak.T
import android.icu.lang.UCharacter.GraphemeClusterBreak.T
import android.icu.lang.UCharacter.GraphemeClusterBreak.T
import android.icu.lang.UCharacter.GraphemeClusterBreak.T










class FViewPager @JvmOverloads constructor (
    context: Context,
    attrs: AttributeSet? = null) : ViewPager (context, attrs) {

    var mCanSlide : Boolean = false

    init {
        offscreenPageLimit = 10
        setPageTransformer(true, FDefaultTransformer())
    }

    override fun onInterceptTouchEvent(ev: MotionEvent): Boolean {
        if (mCanSlide){
            val intercept = super.onInterceptTouchEvent(swapEvent(ev))
            swapEvent(ev)
            return intercept
        }
        return mCanSlide
    }


    override fun setCurrentItem(item: Int) {
        this.setCurrentItem(item, false)
    }

    override fun setCurrentItem(item: Int, smoothScroll: Boolean) {
        super.setCurrentItem(item, smoothScroll)
    }

    override fun onTouchEvent(ev: MotionEvent): Boolean {
        //return false;// 可行,不消费,传给父控件
        //return true;// 可行,消费,拦截事件
        //super.onTouchEvent(ev); //不行,
        //虽然onInterceptTouchEvent中拦截了,
        //但是如果viewpage里面子控件不是viewgroup,还是会调用这个方法.
        return if (mCanSlide) {
            super.onTouchEvent(swapEvent(ev))
        } else {
            true// 可行,消费,拦截事件
        }
    }



    private fun swapEvent(event: MotionEvent): MotionEvent {
        //获取宽高
        val width = width.toFloat()
        val height = height.toFloat()
        //将Y轴的移动距离转变成X轴的移动距离
        val swappedX = event.y / height * width
        //将X轴的移动距离转变成Y轴的移动距离
        val swappedY = event.x / width * height
        //重设event的位置
        event.setLocation(swappedX, swappedY)
        return event
    }
}